import 'package:camera/camera.dart';
import 'package:flutter/cupertino.dart';
import 'package:sqflite/sqflite.dart';

import '../page/bloc/MessageListCubit.dart';
import '../page/bloc/SessionListCubit.dart';
import '../page/model/MessageSendListModel.dart';
import '../pojo/ImPojo.dart';
import '../service/LocalSeqCreater.dart';
import '../socket/ImWebSocketable.dart';
import 'Objects.dart';

class ImSingleton {

  static ImSingleton _instance = ImSingleton._();

  late final ImWebSocketable _imWebSocket;
  Database? dataBase;

  // 当前活动栏目
  SessionListCubit? _sessionListCubit;                         // 会话页
  MessageListCubit? _messageListCubit;                         // 消息页
  MessageSendListModel? _messageSendListModel;
  List<CameraDescription> cameras = [];                        // 摄像头

  final _localSeqCreator = DefaultSeqCreator();                // 序列器
  Session session = Session();                                 // 存储数据
  bool _isWebSocketInstance = false;                           // websocket 是否实例化
  String rootDir = '';                                         // 附件保存附录
  BuildContext? _mainContext;                                  // main.dart页的context;

  // 私有的命名构造函数
  ImSingleton._();

  static ImSingleton getInstance() {
    if (ImSingleton._instance == null) {
      _instance = ImSingleton._();
    }

    return _instance;
  }

  get seqCreator => _localSeqCreator;

  set imWebSocket(ImWebSocketable imWebSocket) {
    _imWebSocket = imWebSocket;
    _isWebSocketInstance = true;
  }

  ImWebSocketable get imWebSocket {
    return _imWebSocket;
  }

  bool get isWebSocketInstance {
    return _isWebSocketInstance;
  }

  set sessionListCubit(SessionListCubit? sessionListCubit) => _sessionListCubit = sessionListCubit;
  SessionListCubit? get sessionListCubit => _sessionListCubit;

  // set hotMessageTotalCubit(HotMessageTotalCubit? hotMessageTotalCubit) => this._hotMessageTotalCubit =
  //     hotMessageTotalCubit;
  // HotMessageTotalCubit? get hotMessageTotalCubit => this._hotMessageTotalCubit;

  set messageListCubit(MessageListCubit? _messageListCubit1) => _messageListCubit = _messageListCubit1;
  MessageListCubit? get messageListCubit => _messageListCubit;

  set messageSendListModel(MessageSendListModel? _messageSendListModel) => _messageSendListModel = _messageSendListModel;
  MessageSendListModel? get messageSendListModel => _messageSendListModel;

  set mainContext(BuildContext? context) => _mainContext = context;
  BuildContext? get mainContext => _mainContext;

  void cleanUiModel() {
    _messageListCubit = null;
  }

  void loginSuccess(String userId, int authCode, String token, String authToken, String tenantCode, DateTime
    serverDate) {
    session.userId = userId;
    session.putData('token', token);
    session.putData('authCode', authCode);
    session.putData('authToken', authToken);
    session.putData('tenantCode', tenantCode);
    session.putData('serverDate', serverDate);
  }
}

class Session {
  String? userId;
  final Map<String, Object> _sessionDataMap = {};

  Session();

  Object? getData(String key) {
    return _sessionDataMap[key];
  }

  void putData(String key, Object data) {
    _sessionDataMap[key] = data;
  }

  bool has(String key) {
    return _sessionDataMap.containsKey(key);
  }

  Object? delete(String key) {
    if (has(key)) {
      return _sessionDataMap.remove(key);
    }
    return null;
  }
}
